//
//  AppDelegate.swift
//  Weibo
//
//  Created by xiongminghua on 16/7/18.
//  Copyright © 2016年 熊明华. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        // 换肤：设置全局导航栏上按钮字体的颜色,需要提前准备好，所以在这里设置
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        //创建主窗口
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        // 设置根控制器
        let rootVC = HMTabBarController()
        rootVC.view.backgroundColor = UIColor.brownColor()
        
//        window?.rootViewController = rootVC
        
//        window?.rootViewController = HMWelcomeViewController()
        
        window?.rootViewController = defaultViewController()
    
        // 设置主窗口并显示
        window?.makeKeyAndVisible()
        
        return true
    }
    
    /// 类方法，给外界调用来切换控制器
    class func swithRootViewController(controller: UIViewController) {
        
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.window?.rootViewController = controller
    }
    
    
    
    /// 程序直接根据条件自动选择进入某个控制器
    private func defaultViewController() -> UIViewController {
        
        // 判断用户是否登录了
        if !HMUserAccountViewModel.sharedUserAccountViewModel.isLogin {
            // 没有登录
            return HMTabBarController()
        } else {
            // 登录了, 再判断是否是新版本
            let newVersion = isNewVersion()
            
            return newVersion ? HMNewFeatureViewController(): HMWelcomeViewController()
        }
        
    }
    
    /// 判断是否为新版本
    private func isNewVersion() -> Bool {
        
        // 获取当前程序的版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        // 从沙盒中获取保存的版本
        let key = "lastVersion"
        let lastVersion = NSUserDefaults.standardUserDefaults().stringForKey(key)
        
        let isNewVersion = currentVersion != lastVersion
        // 如果不同
        if isNewVersion {
            
            // 将新版本号保存到沙盒中
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: key)
            // 同步
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
        return isNewVersion
    }
    

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

